package com.zkingsoft.wechat;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.matrix.core.pojo.AjaxResult;
import com.matrix.core.tools.LogUtil;
import com.matrix.core.tools.StringUtils;
import com.matrix.core.tools.UUIDUtil;
import com.matrix.core.web.BaseAction;
import com.zkingsoft.bean.BizAuth;
import com.zkingsoft.bean.BizShop;
import com.zkingsoft.bean.BizUser;
import com.zkingsoft.common.constance.AppConstance;
import com.zkingsoft.dao.BizAuthDao;
import com.zkingsoft.dao.BizShopDao;
import com.zkingsoft.redis.RedisUserLoginUtils;
import com.zkingsoft.service.BizShopService;
import com.zkingsoft.service.BizUserService;
import com.zkingsoft.tools.AES;
import com.zkingsoft.wechat.vo.DataDecipheringVo;

/**
 * @description 商铺相关接口
 * @author 姜友瑶
 * @date 2018-05-23 00:08
 */
@Controller
@RequestMapping(value = "wxapi/shop")
public class WxShopAction extends BaseAction {

	@Autowired
	private BizUserService bizUserService;

	@Autowired
	private BizShopService bizShopService;

	@Autowired
	private BizShopDao bizShopDao;

	@Autowired
	RedisUserLoginUtils redisUserLoginUtils;

	@Autowired
	BizAuthDao BizAuthDao;

	/**
	 * 手机号码解密
	 * 
	 * @author JIANGYOUYAO
	 * @email 935090232@qq.com
	 * @date 2018年8月28日
	 * @param dataDecipheringVo
	 * @return
	 */
	@RequestMapping(value = "/deciphering")
	public @ResponseBody AjaxResult deciphering(@RequestBody DataDecipheringVo dataDecipheringVo) {
		BizUser loginUser = redisUserLoginUtils.getLoginUser();
		String session_key = loginUser.getSessionKey();
		LogUtil.debug(dataDecipheringVo.toString());
		String jsonResult = AES.wxDecrypt(dataDecipheringVo.getEncryptedData(), session_key, dataDecipheringVo.getIv());
		AjaxResult result = new AjaxResult();
		result.setStatus(AjaxResult.STATUS_SUCCESS);
		result.putInMap("decipher", jsonResult);
		return result;
	}

	/**
	 * 新增认证信息
	 * 
	 * @author JIANGYOUYAO
	 * @email 935090232@qq.com
	 * @date 2018年7月16日
	 * @param bizAuth
	 * @return
	 */
	@RequestMapping(value = "/auth")
	public @ResponseBody AjaxResult auth(@RequestBody BizAuth bizAuth) {
		BizUser loginUser = redisUserLoginUtils.getLoginUser();
		bizAuth.setCreateBy(loginUser.getNickName());
		bizAuth.setUpdateBy(loginUser.getNickName());
		bizAuth.setId(UUIDUtil.getRandomID());
		bizAuth.setUserId(loginUser.getUserId());
		// 设置审核中
		bizAuth.setAuthStatus(AppConstance.AUTHSTATUS_INDG);
		int i = BizAuthDao.insert(bizAuth);
		if (i > 0) {
			return new AjaxResult(AjaxResult.STATUS_SUCCESS, "提交成功");
		} else {
			return new AjaxResult(AjaxResult.STATUS_FAIL, "提交失败");
		}
	}

	/**
	 * 修改审核信息
	 * 
	 * @author JIANGYOUYAO
	 * @email 935090232@qq.com
	 * @date 2018年7月17日
	 * @param bizAuth
	 * @return
	 */
	@RequestMapping(value = "/modifyAuth")
	public @ResponseBody AjaxResult modifyAuth(@RequestBody BizAuth bizAuth) {
		BizUser loginUser = redisUserLoginUtils.getLoginUser();
		bizAuth.setUserId(loginUser.getUserId());
		// 设置审核中
		bizAuth.setAuthStatus(AppConstance.AUTHSTATUS_INDG);
		int i = BizAuthDao.updateByModel(bizAuth);
		if (i > 0) {
			return new AjaxResult(AjaxResult.STATUS_SUCCESS, "修改成功");
		} else {
			return new AjaxResult(AjaxResult.STATUS_FAIL, "修改失败");
		}
	}

	/**
	 * 更新商铺信息
	 * 
	 * @author JIANGYOUYAO
	 * @email 935090232@qq.com
	 * @date 2018年7月16日
	 * @param bizShop
	 * @return
	 */
	@RequestMapping(value = "/updateShop")
	public @ResponseBody AjaxResult updateShop(@RequestBody BizShop bizShop) {
		int i = bizShopService.modifyByModel(bizShop);
		if (i > 0) {
			return new AjaxResult(AjaxResult.STATUS_SUCCESS, "修改成功");
		} else {
			return new AjaxResult(AjaxResult.STATUS_FAIL, "修改失败");
		}
	}

	/**
	 * 注册商铺 1、更新用户的商户部分的信息 2、创建一个默认的商户
	 */
	@RequestMapping(value = "/reginsterShop")
	public @ResponseBody AjaxResult reginsterShop(@RequestBody BizUser bizUser) {
		BizUser loginUser = redisUserLoginUtils.getLoginUser();
		// 更新用户商户信息
		int i = bizUserService.modifyByModel(loginUser);

		if (i > 0) {
			return new AjaxResult(AjaxResult.STATUS_SUCCESS, "注册成功");
		} else {
			return new AjaxResult(AjaxResult.STATUS_FAIL, "注册失败");
		}
	}

	/**
	 * 修改商铺
	 */
	@RequestMapping(value = "/modifyShop")
	public @ResponseBody AjaxResult modifyShop(@RequestBody BizShop shop) {
		 
		int i = bizShopService.modifyByModel(shop);
		if (i > 0) {
			return new AjaxResult(AjaxResult.STATUS_SUCCESS, "商铺修改成功");
		} else {
			return new AjaxResult(AjaxResult.STATUS_FAIL, "商铺修改失败");
		}
	}

	/**
	 * 新增商铺
	 */
	@RequestMapping(value = "/addShop")
	public @ResponseBody AjaxResult addShop(@RequestBody BizShop shop) {
		BizUser loginUser = redisUserLoginUtils.getLoginUser();
		shop.setCreateBy(loginUser.getNickName());
		shop.setUpdateBy(loginUser.getNickName());
		int i = bizShopService.add(shop);
		if (i > 0) {
			return new AjaxResult(AjaxResult.STATUS_SUCCESS, "商铺成新增成功");
		} else {
			return new AjaxResult(AjaxResult.STATUS_FAIL, "商铺成新增失败");
		}
	}

	/**
	 * 删除商铺
	 */
	@RequestMapping(value = "/removeShop/{id}")
	public @ResponseBody AjaxResult removeShop(@PathVariable String id) {
		int i = bizShopService.removeById(id);
		if (i > 0) {
			return new AjaxResult(AjaxResult.STATUS_SUCCESS, "删除成功");
		} else {
			return new AjaxResult(AjaxResult.STATUS_FAIL, "删除失败");
		}
	}

	/**
	 * 查询用户商铺
	 * 
	 * @author JIANGYOUYAO
	 * @email 935090232@qq.com
	 * @date 2018年7月16日
	 */
	@RequestMapping(value = "/findShop")
	public @ResponseBody AjaxResult findShop(@RequestBody BizShop bizShop) {
		return new AjaxResult(AjaxResult.STATUS_SUCCESS, bizShopDao.selectInPageFront(bizShop),
				bizShopDao.selectTotalRecord(bizShop));
	}

}